package org.uu.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.uu.bean.User;
import org.uu.common.CommonResult;
import org.uu.service.UserService;

@RestController
public class UserController {

       @Autowired
       private UserService userService;

       @RequestMapping("/user/add")
       public ResponseEntity addUser(@RequestBody JSONObject userJson, @RequestHeader String token) {
              User user = userJson.toJavaObject(User.class);
              ResponseEntity<String> responseEntity = userService.addUser(user, token);
              return responseEntity;
       }

       @GetMapping("/user/login")
       public ResponseEntity login(@RequestBody JSONObject userJson) {
              User user = userJson.toJavaObject(User.class);
              CommonResult<String> result = new CommonResult<>();
              result.setCode(200);
              result.setMessage("OK");
              String loginToken = userService.login(user);
              result.setData(loginToken);
              return ResponseEntity.ok(result);
       }



}
